<?php
namespace app\manage\controller;

use app\manage\controller\Common;

class AuthrealnameController extends CommonController{

	/**
	 * 实名认证列表
	 */
	public function get_list(){
		$statusArr=$this->getStatus();
		if (request()->isAjax()) {
			$param = request()->param();
        
			$param['limit']     = (isset($param['limit']) and $param['limit']) ? $param['limit'] : 15; // 每页记录数
			$param['page']      = (isset($param['page']) and $param['page']) ? $param['page'] : 1; // 当前页
			$limitOffset        = ($param['page'] - 1) * $param['limit']; // 偏移量
			$param['sortField'] = (isset($param['sortField']) && $param['sortField']) ? $param['sortField'] : 'a.id';
			$param['sortType']  = (isset($param['sortType']) && $param['sortType']) ? $param['sortType'] : 'desc';

            $where="status!=-1 ";
            //用户名
			if(isset($param['username']) && $param['username']){
                $where.=" and b.username like '%{$param['username']}%'";
			}
			//uid
			if(isset($param['uid']) && $param['uid']){
                $where.=" and a.uid = {$param['username']} ";
			}
            //状态
			if(isset($param['status']) && $param['status']){
                $where.=" and a.status = {$param['status']} ";
			}
			
			// 时间
			if(isset($param['datetime_range']) && $param['datetime_range']){
				$dateTime = explode(' - ', $param['datetime_range']);
				$where.=' and a.create_time >='.strtotime($dateTime[0])." and a.create_time <=".strtotime($dateTime[1]);
			}


			//查询符合条件的数据
			$data = db('realname_auth')
            ->alias('a')
            ->field('a.*,b.username')
            ->join('users b','a.uid=b.id','left')
            ->where($where)
            ->order($param['sortField'], $param['sortType'])->limit($limitOffset, $param['limit'])
            ->paginate($param['limit'])
			->toArray();


			
			foreach ($data['data'] as $key => &$value) {
				$card_up=$value['card_up']?explode(',',$value['card_up']):[];
                $card_up_img='';
                foreach($card_up as $v){
                    $card_up_img.="<img src='{$v}' style='width:150px;' />";
                }
				$value['card_up_img']=$card_up_img;
				
				$card_down=$value['card_down']?explode(',',$value['card_down']):[];
                $card_down_img='';
                foreach($card_down as $v){
                    $card_down_img.="<img src='{$v}' style='width:150px;' />";
                }
                $value['card_down_img']=$card_down_img;

				$value['create_time']=date('Y-m-d',$value['create_time']);
				
				$value['statusStr']=$statusArr[$value['status']];
			}

			//权限查询
			// if ($count) $data['power'] = model('ManageUserRole')->getUserPower(['uid'=>session('manage_userid')]);

			return json([
				'code'  => 0,
				'msg'   => '',
				'count' => $data['total'],
				'data'  => $data['data']
			]);
		}

		$this->assign('statusArr',$statusArr);
		return view();
	}
   /**
	 * 详情
	 * @return [type] [description]
	 */
	public function detail(){
		$data=db('realname_auth')->alias('a')
			->field('a.*,b.username')
			->join('users b','a.uid=b.id','left')
			->find();
		$statusArr=$this->getStatus();
		$this->assign('statusArr',$statusArr);
        $this->assign('data',$data);
		return $this->fetch();
	}
	/**
	 * 详情
	 * @return [type] [description]
	 */
	public function check(){
		$id=request()->param('id',0,'intval');
		if(request()->isAjax()){
            return model('authrealname')->check();
		}
		$data= model('authrealname')->find($id);

		$statusArr=$this->getStatus();
		$this->assign('statusArr',$statusArr);
        $this->assign('data',$data);
		return $this->fetch();
	}


    /**
	 * 添加
	 * @return [type] [description]
	 */
	public function add(){
		if(request()->isAjax()){
            $param = input('param.');
            //p($param);
            $user_type= db('users')->where(['id'=>$param['uid']])->value('user_type');
            if($user_type==4){
                return '该用户已经是发行商了';
            }
            $param['create_time']=time();
			$flag=model('authrealname')->allowField (true)->save($param);
            db('users')->where(['id'=>$param['uid']])->update(['user_type'=>4]);
            return $flag;
		}
        
        $users=db('users')->field('id,username')->select();

        $this->assign('users',$users);
		return $this->fetch();
	}

	/**
	 * 编辑
	 * @return [type] [description]
	 */
	public function edit(){
        $id=request()->param('id',0,'intval');
		if(request()->isAjax()){
            $param = input('param.');
            //p($param);
            $uid=db('users_authrealname')->where(['id'=>$id])->value('uid');
            if($uid!=$param['uid']){
                //查看改用户是不是已经是发行商
                $user_type= db('users')->where(['id'=>$param['uid']])->value('user_type');
                if($user_type==4){
                    return '该用户已经是发行商了';
                }
                db('users')->where(['id'=>$uid])->update(['user_type'=>1]);
                db('users')->where(['id'=>$param['uid']])->update(['user_type'=>4]);
            }
			return model('authrealname')->allowField (true)->save($param,$id);
		}
        $data= model('authrealname')->find($id);
        $users=db('users')->field('id,username')->select();
        $creditType=CreditType();

        $this->assign('creditType',$creditType);
        $this->assign('users',$users);
        $this->assign('data',$data);

		return $this->fetch();
	}

    /**
	 * 开关
	 */
	public function update(){
		return model('authrealname')->onOff();
	}
    /**
	 * 删除
	 */
	public function del(){
		return model('authrealname')->del();
	}

  
	/**
	 * nft文件/凭证
	 * @return [type] [description]
	 */
	public function fileUpload(){
		//文件名
		$fileName = input('post.fileName/s');

		//数据验证
		// $validate = validate('app\manage\validate\Bank');
		// if(!$validate->scene('qrcodeUpload')->check(['fileName'=>$fileName])){
		// 	return json(['success'=>$validate->getError()]);
		// }

		//二维码图片
		$file = request()->file('file');

		//上传路径
		$uploadPath = './upload/file/nft';
		if(!is_dir($uploadPath)) mkdir($uploadPath, 0777, true);

		$info = $file->validate(['size'=>1000*1024*5,'ext'=>'jpg,png,gif,jpeg'])->rule('date')->move($uploadPath, $fileName);
		if($info){
			// 成功上传后 获取上传信息
			return json(['success'=>ltrim($uploadPath, '.').'/'.$info->getSaveName()]);
		}else{
			// 上传失败获取错误信息
			return json(['success'=>$file->getError()]);
		}
	}
	/**
	 * Undocumented function
	 *
	 * @return void
	 */
	private function getStatus(){
		return [
			1=>'审核中',
			2=>'审核通过',
			3=>'审核失败'
		];
	}

}